package com.wang.web;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wang.pojo.HomeCost;
import com.wang.pojo.Page;
import com.wang.pojo.User;
import com.wang.service.HomeCostService;
import com.wang.utils.WebUtils;

/**
 * 访问地址url:localhost:8080/homeCost/manager/homeCostServlet
 * Servlet implementation class HomeCostServlet
 */
@WebServlet("/manager/homeCostServlet")
public class HomeCostServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private HomeCostService homeCostService = new HomeCostService();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("add".equals(action)) {
            add(request, response);
        } else if ("delete".equals(action)) {
            delete(request, response);
        } else if ("update".equals(action)) {
            update(request, response);
        } else if ("getHomeCostById".equals(action)) {
            getHomeCostById(request, response);
        } else if ("pageByKeyword".equals(action)) {
            pageByKeyword(request, response);
        } else if ("page".equals(action)) {
            page(request, response);
        }
    }

    //添加消费记录
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取网页提交的参数
        String costName = request.getParameter("costName");
        BigDecimal money = WebUtils.bigdecimal(request.getParameter("money"), new BigDecimal(0.00));
        String remark = request.getParameter("remark");
        User user = (User) request.getSession().getAttribute("user");
        costName = costName.trim();
        remark = remark.trim();
        //封装成类对象
        HomeCost homeCost = new HomeCost(costName, money, remark, user.getId());
        //执行添加操作，返回1,添加成功，反之失败
        if (homeCostService.add(homeCost) == 1) {
            //页面重定向
            Page page = (Page) request.getSession().getAttribute("page");
            response.sendRedirect(request.getContextPath() + "/manager/homeCostServlet?action=page&pageNo=" + (page.getPageTotal() + 1) + "&pageSize=" + page.getPageSize());
        } else {
            //请求转发
            request.setAttribute("msg", "添加失败，联系管理员");
            request.setAttribute("homeCost", homeCost);
            request.getRequestDispatcher("/pages/cost_edit.jsp").forward(request, response);
        }
    }

    //删除消费记录
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取id
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        //执行删除操作，返回1,删除成功，反之失败
        if (homeCostService.delete(id) == 1) {
            //页面重定向
            int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
            int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), 4);
            response.sendRedirect(request.getContextPath() + "/manager/homeCostServlet?action=page&pageNo=" + pageNo + "&pageSize=" + pageSize);
        } else {
            //请求转发
            request.setAttribute("msg", "删除失败，联系管理员");
            request.getRequestDispatcher("/manager/homeCostServlet?action=page").forward(request, response);
        }

    }

    //修改消费记录
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        String costName = request.getParameter("costName");
        BigDecimal money = WebUtils.bigdecimal(request.getParameter("money"), new BigDecimal(0.00));
        String date = request.getParameter("date");
        String remark = request.getParameter("remark");
        costName = costName.trim();
        remark = remark.trim();
        //封装
        HomeCost homeCost = new HomeCost(id, costName, money, date, remark);
        //执行删除操作，返回1,修改成功，反之失败
        if (homeCostService.update(homeCost) == 1) {
            int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
            int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), 4);
            response.sendRedirect(request.getContextPath() + "/manager/homeCostServlet?action=page&pageNo=" + pageNo + "&pageSize=" + pageSize);
        } else {
            request.setAttribute("msg", "修改失败，联系管理员");
            request.setAttribute("homeCost", homeCost);
            request.getRequestDispatcher("/pages/cost_edit.jsp").forward(request, response);
        }
    }


    //通过id查询该条消费记录
    private void getHomeCostById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        HomeCost homeCost = homeCostService.getHomeCostById(id);
        System.out.println(homeCost);
        request.setAttribute("homeCost", homeCost);
        request.getRequestDispatcher("/pages/cost_edit.jsp").forward(request, response);
    }

    //通过关键词查询
    protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        List<HomeCost> homeCost = homeCostService.query(keyword);
        request.setAttribute("homeCost", homeCost);
        request.getRequestDispatcher("/pages/query.jsp").forward(request, response);

    }

    protected void pageByKeyword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String keyword = req.getParameter("keyword");

        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);

        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);

        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            //没登录就访问
            System.out.println("没有登录");
            resp.sendRedirect(req.getContextPath() + "/index.jsp");
        } else {
            Page<HomeCost> page = homeCostService.pageByKeyword(pageNo, pageSize, keyword, user);

            page.setPageUrl(req.getContextPath() + "/manager/homeCostServlet?action=pageByKeyword&keyword=" + keyword);
            req.setAttribute("page", page);
            req.getSession().setAttribute("page", page);
            req.getRequestDispatcher("/pages/query.jsp").forward(req, resp);
        }

    }

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);

        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //如果没提供user_id参数，则默认为0，展示所有可见用户的数据
        int user_id = WebUtils.parseInt(req.getParameter("user_id"), 0);

        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            //没登录就访问
            resp.sendRedirect(req.getContextPath() + "/index.jsp");
        } else {
            //第一个user是登录的user，第二个user_id是被选中的user
            Page<HomeCost> page = homeCostService.page(pageNo, pageSize, user, user_id);
            if (user_id == 0) {
                page.setPageUrl(req.getContextPath() + "/manager/homeCostServlet?action=page");
            } else {
                page.setPageUrl(req.getContextPath() + "/manager/homeCostServlet?action=page&user_id=" + user_id);
            }

            req.setAttribute("page", page);
            req.getSession().setAttribute("page", page);

            req.getRequestDispatcher("/pages/manager.jsp").forward(req, resp);
        }
    }

}
